#include "bsp_gpt.h"
#include "bsp_init.h"
#include "bsp_led.h"

void gpt1_init(unsigned int prescaler , unsigned int gpt1_ocr1_data){
    GPT1->CR = 0;
    GPT1->PR = 0;
    GPT1->IR = 0;

    GPT1->CR |= (1<<1) | (1<<6);
    GPT1->PR = prescaler;
    GPT1->IR |= (1<<0);
    GPT1->OCR[0] = gpt1_ocr1_data;
    
    GIC_EnableIRQ(GPT1_IRQn);

    system_register_irqhandler(GPT1_IRQn , gpt1_ocr1_irqhandler, NULL);

    GPT1->CR |= (1<<0);

}

void gpt1_ocr1_irqhandler (unsigned int gicciar, void *param){

    static unsigned char state = 0;

    if( (GPT1->SR & 1) == 1 ){
        state =! state;
        if(state == 0){
            led_close();
        }
        else led_open();

        GPT1->SR |= (1<<0);

    }
}